package com.ergwang.controller.mall;

import com.ergwang.Model.Result;
import com.ergwang.Model.mall.vo.GetOrderByStateVO;
import com.ergwang.service.mail.Order_MallService;
import com.ergwang.service.mail.Order_MallServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author：ergwang
 * @Date: 2020/6/2
 * @Time: 14:16
 */
@WebServlet("/api/mall/order/*")
public class OrderServlet extends HttpServlet {

    private Order_MallService order_mallService = new Order_MallServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/order/", "");
        if ("".equals(action)){

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/order/", "");
        if ("getOrderByState".equals(action)){
            getOrderByState(request,response);
        }
    }

    /**
     * 个人中心的购物车
     * @param request
     * @param response
     */
    private void getOrderByState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String stateId = request.getParameter("state");
        String nickname = request.getParameter("token");
        List<GetOrderByStateVO> orderByStateVO = null;
        if (Integer.parseInt(stateId) == -1){
            orderByStateVO = order_mallService.getOrderByNickName(nickname);
        }else {
            orderByStateVO = order_mallService.getOrderByState(stateId, nickname);
        }
        response.getWriter().println(gson.toJson(Result.ok(orderByStateVO)));

    }
}
